<?php
if (!defined('PATH'))
	exit();

$players_deaths = $this->db->query('SELECT `player_deaths`.`id`, `player_deaths`.`date`, `player_deaths`.`level`, `players`.`name`, `players`.`world_id` FROM `player_deaths` LEFT JOIN `players` ON `player_deaths`.`player_id` = `players`.`id` ORDER BY `date` DESC LIMIT 0,' . $limit);
$players_deaths_count = 0;
$players_rows = null;
if (!empty($players_deaths)) {
	foreach ($players_deaths as $death) {
		$players_deaths_count++;

		$players_rows .= '<tr><td><center>' . $players_deaths_count . '.</center></td><td><small>' . date($this->config['website']['date_format'], $death['date']) . '</small></td><td><a href="' . PATH . '/search/check/' . urlencode($death['name']) . '/"><b>' . $death['name'] . '</b></a> ';
		$killers = $this->db->query("SELECT environment_killers.name AS monster_name, players.name AS player_name, players.deleted AS player_exists FROM killers LEFT JOIN environment_killers ON killers.id = environment_killers.kill_id LEFT JOIN player_killers ON killers.id = player_killers.kill_id LEFT JOIN players ON players.id = player_killers.player_id WHERE killers.death_id = '" . $death['id'] . "' ORDER BY killers.final_hit DESC, killers.id ASC")->fetchAll();

		$i = 0;
		$count = count($killers);
		foreach ($killers as $killer) {
			$i++;
			if ($killer['player_name'] != "") {
				if ($i == 1)
					$players_rows .= "killed at level <b>" . $death['level'] . "</b>";
				else if ($i == $count)
					$players_rows .= " and";
				else
					$players_rows .= ",";

				$players_rows .= " by ";
				if ($killer['monster_name'] != "")
					$players_rows .= $killer['monster_name'] . " summoned by ";

				if ($killer['player_exists'] == 0)
					$players_rows .= "<a href=\"" . PATH . "/search/check/" . urlencode($killer['player_name']) . "/\">";

				$players_rows .= $killer['player_name'];
				if ($killer['player_exists'] == 0)
					$players_rows .= "</a>";
			}
			else {
				if ($i == 1)
					$players_rows .= "died at level <b>" . $death['level'] . "</b>";
				else if ($i == $count)
					$players_rows .= " and";
				else
					$players_rows .= ",";

				$players_rows .= " by " . $killer['monster_name'];
			}
		}

		$players_rows .= '.</td></tr>';
	}
}

if ($players_deaths_count == 0)
	$this->archez->showMsg('No one died on <b>' . $this->config['server']['name'] . '</b>.', 'notice');
else {
?>
<table border="0" cellpadding="8" cellspacing="1" width="100%">
<tr class="head"><td colspan="4"><b><?=$tableName?></b></td></tr>
<tr class="bold"><td width="5%">#</td><td width="30%">Date</td><td width="65%">Information</td></tr>
<?=$players_rows?>
</table>
<?php
}
?>